local lsp = require('lspconfig')
local cmp = require('cmp')
local cmp_lsp = require('cmp_nvim_lsp')

local let = vim.g

-- Nvim LSP
let.completion_matching_strategy_list = {'exact', 'fuzzy', 'substring'}

-- Enable language servers here
lsp.clangd.setup({ -- Clangd for c/c++
    capabilities=cmp_lsp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
})
lsp.pylsp.setup({ -- PyLSP for python
    capabilities=cmp_lsp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
})
lsp.solargraph.setup({ -- Solargraph for ruby
    capabilities=cmp_lsp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
})
lsp.zls.setup({
    capabilities=cmp_lsp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
})
-- Ale config (linters)
let.ale_linters = {
    cpp = {'clangd', 'clang', 'clang-tidy', 'clang-check', 'cppcheck'},
    c = {'clangd', 'clang', 'clang-tidy', 'clang-check'},
    ruby = {'ruby', 'solargraph'},
    python = {'pylint', 'flake8'},
    sh = {'shellcheck'},
    lua = {'luac'},
    json = {'jq'},
}

-- Ale config (formaters)
let.ale_fixers = {
    all = {'trim_whitespace'},
--     cpp = {'clang-format'},
--    c = {'clang-format'},
    python = {'black'},
}

-- Ale config (variables)
let.ale_completion_autoimport = 1
let.ale_completion_enabled = 0
let.ale_linters_explicit = 1
let.ale_set_highlights = 1
let.ale_lint_on_enter = 1
let.ale_set_quickfix = 1
let.ale_fix_on_save = 1
let.ale_sign_warning = '!!'
let.ale_sign_error = 'XX'
--highlight clear ALEErrorSign guifg=Red
--highlight clear ALEWarningSign guifg=Yellow

-- Tree sitter config
local parser_config = require("nvim-treesitter.parsers").get_parser_configs()

parser_config.org = {
  install_info = {
    url = 'https://github.com/milisims/tree-sitter-org',
    revision = 'main',
    files = {'src/parser.c', 'src/scanner.cc'},
  },
  filetype = 'org',
}

require('nvim-treesitter.configs').setup {
  -- If TS highlights are not enabled at all, or disabled via `disable` prop, highlighting will fallback to default Vim syntax highlighting
  highlight = {
    enable = true,
    disable = {'org'}, -- Remove this to use TS highlighter for some of the highlights (Experimental)
    additional_vim_regex_highlighting = {'org'}, -- Required since TS highlighter doesn't support all syntax features (conceal)
  },
  ensure_installed = {'org'}, -- Or run :TSUpdate org
}

local completion_icons = {
  Text = "",
  Method = "",
  Function = "",
  Constructor = "",
  Field = "",
  Variable = "",
  Class = "ﴯ",
  Interface = "",
  Module = "",
  Property = "ﰠ",
  Unit = "",
  Value = "",
  Enum = "",
  Keyword = "",
  Snippet = "",
  Color = "",
  File = "",
  Reference = "",
  Folder = "",
  EnumMember = "",
  Constant = "",
  Struct = "",
  Event = "",
  Operator = "",
  TypeParameter = ""
}

cmp.setup({ -- Define how completion engine works
    mapping = { -- Keymaps for completion
        ['<CR>'] = cmp.mapping.confirm({ select = true }),
        ['<A-k>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
        ['<A-j>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),
        ['<Down>'] = cmp.mapping.close(),
        ['<Up>'] = cmp.mapping.close()
    },
    formatting = { -- Result formatting, FIXME: Why does extra text still appear?
        format = function(entry, vim_item)
            vim_item.kind = string.format('%s %s', completion_icons[vim_item.kind], vim_item.kind)
            vim_item.menu = ({
                path = "Path",
                buffer = "ABC",
                nvim_lsp = "LSP",
            })[entry.source.name]
            return vim_item
        end 
    },
    sources = { -- Sources to pull from for completion
        { name = 'nvim_lsp', max_item_count = 5},
        { name = 'buffer', max_item_count = 3 },
        { name = 'path', max_item_count = 3 },
        { name = 'org', max_item_count = 3 },
    }
})

